Skip to content

feat(infinity-agent-core): dispatch tool calls eagerly, before the model stream ends - #191

Draft
shadaj wants to merge 1 commit into
sandbox-85094202-d192-4d34-8a56-cbb5baf08cf0from
sandbox-28c32af1-38b5-40da-be90-a451bb38ad66
Draft

shadaj wants to merge 1 commit into
sandbox-85094202-d192-4d34-8a56-cbb5baf08cf0from
sandbox-28c32af1-38b5-40da-be90-a451bb38ad66

Conversation

@shadaj

@shadaj shadaj commented Nov 9, 2026

Copy link
Copy Markdown
Member

Previously a step collected the model's ExecuteToolCall action and only
dispatched it after the completion stream had fully ended (Final chunk
received), then sync()ed, then fired the tool. The tool's latency was
therefore added after the model's tail instead of overlapping it.

run_completion (event_processor.rs)

  • Before yielding CompletionAction::ExecuteToolCall, the stream now
    explicitly mark_inputs_model_validated()s and history.sync().await?s
    (the same pattern the synchronous-tool path already used). A yielded
    tool call is therefore already durable and may be dispatched the
    moment it is received — the low-level API's "sync before dispatch"
    contract is now satisfied by construction rather than left to callers.
  • Doc comments on CompletionAction, CompletionEvent::Action and
    execute_action updated to describe the new contract: ExecuteToolCall
    is not a terminal event, it is yielded as soon as the call is complete.

Thread::step_no_defer (system/thread.rs)

  • The "collect one action, dispatch after the stream" flow is replaced by a
    tokio::select! loop over the completion stream and a FuturesUnordered
    of in-flight dispatches. When the stream yields an ExecuteToolCall, the
    dispatch (execute_action_with_error_result) is pushed into the set and
    runs concurrently while the rest of the model stream (the Final chunk
    carrying usage, any ignored concurrent tool calls) is drained.
  • The stream is the queue of actions: a FuturesUnordered rather than a
    single Option slot means that once run_completion starts yielding
    several calls per turn (parallel tool calls), they will all dispatch
    eagerly with no further changes to the invoker.
  • All dispatches are drained before the step returns, so a step never
    leaves a dispatch in flight (matching the old guarantee that the step
    future completes only after the tool has been fired), and the post-stream
    sync() + CompletionFinished ordering is unchanged.
  • handle_completion_event now returns Option<CompletionAction> instead
    of writing into an &mut Option with a "multiple terminal actions"
    assertion.

Tests

  • New tool_call_dispatches_before_stream_finishes: emits a tool call from
    the mock model without finishing the stream and asserts that the tool's
    execute runs, that the call is already in the conversation store at that
    point, and that no CompletionFinished has been emitted yet. Verified to
    fail against the old dispatch-after-stream behaviour.

Docs

  • low-level/completion-loop.md, low-level/history-manager.md,
    architecture.md: describe eager dispatch and that run_completion syncs
    before yielding ExecuteToolCall.

./check.bash (fmt, clippy, all tests, doctests) passes.

…del stream ends

Previously a step collected the model's `ExecuteToolCall` action and only
dispatched it after the completion stream had fully ended (`Final` chunk
received), then `sync()`ed, then fired the tool. The tool's latency was
therefore added *after* the model's tail instead of overlapping it.

## `run_completion` (`event_processor.rs`)

* Before yielding `CompletionAction::ExecuteToolCall`, the stream now
  explicitly `mark_inputs_model_validated()`s and `history.sync().await?`s
  (the same pattern the synchronous-tool path already used). A yielded
  tool call is therefore **already durable** and may be dispatched the
  moment it is received — the low-level API's "sync before dispatch"
  contract is now satisfied by construction rather than left to callers.
* Doc comments on `CompletionAction`, `CompletionEvent::Action` and
  `execute_action` updated to describe the new contract: `ExecuteToolCall`
  is not a terminal event, it is yielded as soon as the call is complete.

## `Thread::step_no_defer` (`system/thread.rs`)

* The "collect one action, dispatch after the stream" flow is replaced by a
  `tokio::select!` loop over the completion stream and a `FuturesUnordered`
  of in-flight dispatches. When the stream yields an `ExecuteToolCall`, the
  dispatch (`execute_action_with_error_result`) is pushed into the set and
  runs concurrently while the rest of the model stream (the `Final` chunk
  carrying usage, any ignored concurrent tool calls) is drained.
* The stream *is* the queue of actions: a `FuturesUnordered` rather than a
  single `Option` slot means that once `run_completion` starts yielding
  several calls per turn (parallel tool calls), they will all dispatch
  eagerly with no further changes to the invoker.
* All dispatches are drained before the step returns, so a step never
  leaves a dispatch in flight (matching the old guarantee that the step
  future completes only after the tool has been fired), and the post-stream
  `sync()` + `CompletionFinished` ordering is unchanged.
* `handle_completion_event` now returns `Option<CompletionAction>` instead
  of writing into an `&mut Option` with a "multiple terminal actions"
  assertion.

## Tests

* New `tool_call_dispatches_before_stream_finishes`: emits a tool call from
  the mock model *without* finishing the stream and asserts that the tool's
  `execute` runs, that the call is already in the conversation store at that
  point, and that no `CompletionFinished` has been emitted yet. Verified to
  fail against the old dispatch-after-stream behaviour.

## Docs

* `low-level/completion-loop.md`, `low-level/history-manager.md`,
  `architecture.md`: describe eager dispatch and that `run_completion` syncs
  before yielding `ExecuteToolCall`.

`./check.bash` (fmt, clippy, all tests, doctests) passes.

Co-authored-by: Infinity 🤖 <infinity@hydro.run>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Deploying infinity with  Cloudflare Pages  Cloudflare Pages

Latest commit: 12db1f4
Status: ✅  Deploy successful!
Preview URL: https://aec168cb.infinity-dc7.pages.dev
Branch Preview URL: https://sandbox-28c32af1-38b5-40da-b.infinity-dc7.pages.dev

View logs

@shadaj
shadaj added this pull request to stack #192 November 9, 2026 20:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant